package com.android.webserver.manager;

import com.android.webserver.R;
import com.android.webserver.db.DatabaseOpenHelper;
import com.android.webserver.db.WebApplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * @author Baptiste GOURDIN
 *
 */
public class WebAppDetail extends Activity 
{
	String appPath = "";
	private String networkRes[] = {"Wi-Fi", "3G", "All"};
	private String locationRes[] = {"Home", "Work", "All"};
	int numParams = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.webappdetail);
		setContentView(R.layout.appconfigure);

		Intent app = getIntent();
		appPath = app.getStringExtra("path");
		Log.i("WebServer", "Got path = " + appPath);
		DatabaseOpenHelper db = new DatabaseOpenHelper(this);
		WebApplication webapp = db.getWebApplicationFromPath(appPath);
		((TextView)findViewById(R.id.appTitle)).setText(webapp.getName());
		((TextView)findViewById(R.id.appPath)).setText(webapp.getPath());

		Spinner s = (Spinner) findViewById(R.id.nwRestrictions);
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, networkRes);
        s.setAdapter(adapter);

        s = (Spinner) findViewById(R.id.locRestrictions);
        adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, locationRes);
        s.setAdapter(adapter);
        
		ConfigHandler ch = new ConfigHandler(webapp, db);
		ToggleButton toggle = null;

		toggle = (ToggleButton)findViewById(R.id.httponlyOption);
		if (webapp.getHttpOnlyStatus()) {
			toggle.setChecked(true);
			numParams++;
		}else 
			toggle.setChecked(false);
		toggle.setOnCheckedChangeListener(ch);
		
		toggle = (ToggleButton)findViewById(R.id.serverfilterOption);
		if (webapp.getServerFilterStatus()) {
			toggle.setChecked(true);
			numParams++;
		} else 
			toggle.setChecked(false);
		toggle.setOnCheckedChangeListener(ch);
		
		toggle = (ToggleButton)findViewById(R.id.cspOption);
		if (webapp.getCspStatus()) {
			toggle.setChecked(true);
			numParams++;
		} else 
			toggle.setChecked(false);
		toggle.setOnCheckedChangeListener(ch);
		
		toggle = (ToggleButton)findViewById(R.id.servercspOption);
		if (webapp.getScspStatus()) {
			toggle.setChecked(true);
			numParams++;
		} else 
			toggle.setChecked(false);
		toggle.setOnCheckedChangeListener(ch);
		
		toggle = (ToggleButton)findViewById(R.id.csrfOption);
		if (webapp.getCsrfStatus()) {
			toggle.setChecked(true);
			numParams++;
		} else 
			toggle.setChecked(false);
		toggle.setOnCheckedChangeListener(ch);
		
		toggle = (ToggleButton)findViewById(R.id.originHeaderOption);
		if (webapp.getOriginHeaderStatus()) {
			toggle.setChecked(true);
			numParams++;
		} else 
			toggle.setChecked(false);
		toggle.setOnCheckedChangeListener(ch);
		
		toggle = (ToggleButton)findViewById(R.id.xframeOption);
		if (webapp.getXframeStatus()) {
			toggle.setChecked(true);
			numParams++;
		} else 
			toggle.setChecked(false);
		toggle.setOnCheckedChangeListener(ch);
		
		toggle = (ToggleButton)findViewById(R.id.sslOption);
		if (webapp.getSslStatus()) {
			toggle.setChecked(true);
			numParams++;
		} else 
			toggle.setChecked(false);
		toggle.setOnCheckedChangeListener(ch);
		
		toggle = (ToggleButton)findViewById(R.id.hstsOption);
		if (webapp.getHstsStatus()) {
			toggle.setChecked(true);
			numParams++;
		} else 
			toggle.setChecked(false);
		toggle.setOnCheckedChangeListener(ch);

		toggle = (ToggleButton)findViewById(R.id.securecookieOption);
		if (webapp.getSecureCookieStatus()) {
			toggle.setChecked(true);
			numParams++;
		} else 
			toggle.setChecked(true);
		toggle.setOnCheckedChangeListener(ch);
		
	}

	private void updateSecurityLevel () {
		TextView status = (TextView) findViewById(R.id.securityLevel);
		
		if (numParams >= 8)
			status.setText(" High ");
		else if (numParams >= 5)
			status.setText(" Moderate ");
		else
			status.setText(" Low ");
	}
	
	private class ConfigHandler implements CheckBox.OnCheckedChangeListener {

		WebApplication webapp;
		DatabaseOpenHelper db;
		
		ConfigHandler(WebApplication app, DatabaseOpenHelper helper) {
			webapp = app;
			db = helper;
		}
		
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if (isChecked)
				numParams++;
			else
				numParams--;
			updateSecurityLevel();
			// TODO Auto-generated method stub
			Log.i("WebServer", "clicked checkbox = " + buttonView.getId());
			if (buttonView.getId() == R.id.httponlyOption) {
				webapp.setHttpOnlyStatus(isChecked);
			} else if (buttonView.getId() == R.id.serverfilterOption) {
				webapp.setServerFilterStatus(isChecked);
			} else if (buttonView.getId() == R.id.cspOption) {
				webapp.setCspStatus(isChecked);
			} else if (buttonView.getId() == R.id.servercspOption) {
				webapp.setScspStatus(isChecked);
			} else if (buttonView.getId() == R.id.csrfOption) {
				webapp.setCsrfStatus(isChecked);
			} else if (buttonView.getId() == R.id.originHeaderOption) {
				webapp.setOriginHeaderStatus(isChecked);
			} else if (buttonView.getId() == R.id.xframeOption) {
				webapp.setXframeStatus(isChecked);
			} else if (buttonView.getId() == R.id.hstsOption) {
				webapp.setHstsStatus(isChecked);
			} else if (buttonView.getId() == R.id.securecookieOption) {
				webapp.setSecureCookieStatus(isChecked);
			}
			db.updateApplication(webapp);
		}
	}
}
